package mongodb

import (
	"go_api/pkg/setting"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"log"
	"time"
)

var mgoDb *mgo.Database

type MgoModel struct {
	Id         bson.ObjectId        `bson:"_id" json:"_id"`
	CreatedAt  time.Time          	`bson:"created_at" json:"created_at"`
	UpdatedAt  time.Time            `bson:"updated_at"`
	DeletedAt  time.Time           	`bson:"deleted_at"`
}

func SetupMgo(){
	var err error
	session, err := mgo.DialWithTimeout(setting.MongoSetting.Host, 5*time.Second)
	if err != nil {
		log.Fatalf("mongodb.Setup  session err: %v", err)
	}
	session.SetMode(mgo.Monotonic, true)
	mgoDb = session.DB(setting.MongoSetting.DbName)

	err = mgoDb.Login(setting.MongoSetting.User, setting.MongoSetting.Password)
	if err != nil {
		log.Fatalf("mongodb.Setup err: %v", err)
	}
}